// 业务员
const {userledger,sequelize,salesmanInfo} = require('../../dbConfig/index.js')
// 注册业务员用户信息
const registerSalesman = async (ctx,next) => {
	const body = ctx.request.body
	if(body.user_name){
		const jiance = await userledger.findAll({where:{user_name:body.user_name}})
		if(jiance.length){
			ctx.body = {code:1,data:null,msg:'用户名以被使用'}
			return
		}
	}
	const transaction = await sequelize.transaction();
	const user = await userledger.create({ user_name:body.user_name, user_password:'25d55ad283aa400af464c76d713c07ad',is_flg:body.is_flg,creator_id:ctx.state.user_id }, { transaction });
	await salesmanInfo.create({ user_phone: body.user_phone, full_name:body.full_name,user_id: user.Id}, { transaction });
	await transaction.commit();
	ctx.body = {
		code:200,
		data:null,
		msg:"注册成功,密码:12345678,请尽快修改密码"
	}
}
//删除业务员
const removeSalesman = async (ctx,next)=>{
	const transaction = await sequelize.transaction();
	await salesmanInfo.destroy({where:{user_id:ctx.request.body.id}},{ transaction })
	await userledger.destroy({where:{Id:ctx.request.body.id}},{ transaction })
	transaction.commit()
	ctx.body={
		code:200,
		data:null,
		msg:'成功'
	}
}
//获取业务员列表  TODO后续关联查询业务员业绩表
const getSalesman = async (ctx,next)=>{
	const res = await salesmanInfo.findAll()
	ctx.body={
		code:200,
		data:res,
		msg:'成功'
	}
}
module.exports = {
	registerSalesman,
	getSalesman,
	removeSalesman
}